package com.goldze.mvvmhabit.app;

import android.annotation.SuppressLint;
import android.app.Application;
import android.arch.lifecycle.ViewModel;
import android.arch.lifecycle.ViewModelProvider;
import android.support.annotation.NonNull;
import android.support.annotation.VisibleForTesting;

import com.goldze.mvvmhabit.data.DemoRepository;
import com.goldze.mvvmhabit.ui.base_module.list.SampleListViewModel;
import com.goldze.mvvmhabit.ui.base_module.normal.SampleViewModel;
import com.goldze.mvvmhabit.ui.changePsw.ChangePsdViewModel;
import com.goldze.mvvmhabit.ui.chat.ChatContentListViewModel;
import com.goldze.mvvmhabit.ui.demo.DemoViewModel;
import com.goldze.mvvmhabit.ui.forgetPsw.ForgetPsdViewModel;
import com.goldze.mvvmhabit.ui.leave_recording.LeaveRecordingListViewModel;
import com.goldze.mvvmhabit.ui.login.LoginViewModel;
import com.goldze.mvvmhabit.ui.main.MainViewModel;
import com.goldze.mvvmhabit.ui.network.NetWorkViewModel;
import com.goldze.mvvmhabit.ui.quicklogin.QuickRegisterViewModel;
import com.goldze.mvvmhabit.ui.selectsdepartment.SelectDepartmentViewModel;
import com.goldze.mvvmhabit.ui.splash.SplashViewModel;
import com.goldze.mvvmhabit.ui.tab_bar.fragment.chat.ChatViewModel;
import com.goldze.mvvmhabit.ui.tab_bar.fragment.leave.LeaveViewModel;
import com.goldze.mvvmhabit.ui.tab_bar.fragment.my.MyFragmentViewModel;
import com.goldze.mvvmhabit.ui.vp_frg.ViewPagerGroupViewModel;

/**
 * Created by goldze on 2019/3/26.
 */
public class AppViewModelFactory extends ViewModelProvider.NewInstanceFactory {
    @SuppressLint("StaticFieldLeak")
    private static volatile AppViewModelFactory INSTANCE;
    private final Application mApplication;
    private final DemoRepository mRepository;

    public static AppViewModelFactory getInstance(Application application) {
        if (INSTANCE == null) {
            synchronized (AppViewModelFactory.class) {
                if (INSTANCE == null) {
                    INSTANCE = new AppViewModelFactory(application, Injection.provideDemoRepository());
                }
            }
        }
        return INSTANCE;
    }

    @VisibleForTesting
    public static void destroyInstance() {
        INSTANCE = null;
    }

    private AppViewModelFactory(Application application, DemoRepository repository) {
        this.mApplication = application;
        this.mRepository = repository;
    }

    @NonNull
    @Override
    public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
        if (modelClass.isAssignableFrom(NetWorkViewModel.class)) {
            return (T) new NetWorkViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(DemoViewModel.class)) {
            return (T) new DemoViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(LoginViewModel.class)) {
            return (T) new LoginViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(ChangePsdViewModel.class)) {
            return (T) new ChangePsdViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(QuickRegisterViewModel.class)) {
            return (T) new QuickRegisterViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(MainViewModel.class)) {
            return (T) new MainViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(MainViewModel.class)) {
            return (T) new MainViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(SelectDepartmentViewModel.class)) {
            return (T) new SelectDepartmentViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(MyFragmentViewModel.class)) {
            return (T) new MyFragmentViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(LeaveViewModel.class)) {
            return (T) new LeaveViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(LeaveRecordingListViewModel.class)) {
            return (T) new LeaveRecordingListViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(ChatViewModel.class)) {
            return (T) new ChatViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(ChatContentListViewModel.class)) {
            return (T) new ChatContentListViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(ViewPagerGroupViewModel.class)) {
            return (T) new ViewPagerGroupViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(SplashViewModel.class)) {
            return (T) new SplashViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(ForgetPsdViewModel.class)) {
            return (T) new ForgetPsdViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(SampleViewModel.class)) {
            return (T) new SampleViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(SampleListViewModel.class)) {
            return (T) new SampleListViewModel(mApplication, mRepository);
        }

        throw new IllegalArgumentException("Unknown ViewModel class: " + modelClass.getName());
    }
}
